﻿using Gx.Common;
using Gx.Common.Core;
using Gx.Dal.Manage;
using Gx.Model;
using Gx.Model.Entity;
using Gx.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;


namespace Gx.Bll.Manage
{
    public class LKBillTemplateBll
    {
        protected LKBillTemplateDal dal = new LKBillTemplateDal();
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public List<LKBillTemplate> GetLKBillTemplates()
        {
            return dal.GetAll().ToList();
        }
        /// <summary>
        /// 保存模板
        /// </summary>
        /// <param name="lKBillTemplate"></param>
        /// <returns></returns>
        public ReturnModel SaveLKBillTemplate(LKBillTemplate lKBillTemplate)
        {
            var template = GetLKBillTemplateByBillType(lKBillTemplate.BillType);

            if (template == null)
            {
                return AddLKBillTemplate(lKBillTemplate);
            }
            else
            {
                lKBillTemplate.Id = template.Id;
                return new ReturnModel { Success = dal.Update(lKBillTemplate), Msg = "编辑成功" };
            }
        }

        /// <summary>
        /// 新增单据模板
        /// </summary>
        /// <param name="lKBillTemplate"></param>
        /// <returns></returns>
        public ReturnModel AddLKBillTemplate(LKBillTemplate lKBillTemplate)
        {


            lKBillTemplate.CreateTime = DateTime.Now;
            lKBillTemplate.CreateUserId = LoginHelper.GetUserId();
            lKBillTemplate.CreateUser = LoginHelper.UserName();
            var result = dal.Insert(lKBillTemplate);
            return new ReturnModel { Success = result, Msg = result ? "新增成功" : "新增失败" };
        }

        /// <summary>
        /// 编辑单据模板
        /// </summary>
        /// <param name="lKBillTemplate"></param>
        /// <returns></returns>
        public bool EditLKBillTemplate(LKBillTemplate lKBillTemplate)
        {
            var cus = dal.Get(lKBillTemplate.Id);
            if (cus == null)
            {
                return false;
            }
            lKBillTemplate.CreateTime = cus.CreateTime;
            lKBillTemplate.CreateUserId = cus.CreateUserId;
            lKBillTemplate.CreateUser = cus.CreateUser;
            //lKBillTemplate.ModifyUserId = LoginHelper.GetUserId();
            //lKBillTemplate.ModifyUser = LoginHelper.UserName();
            //lKBillTemplate.ModifyTime = DateTime.Now;
            return dal.Update(lKBillTemplate);
        }

        /// <summary>
        /// 删除单据模板
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteLKBillTemplate(int id)
        {
            return dal.DeleteById(id);
        }

        //根据billtype获取模板
        public LKBillTemplate GetLKBillTemplateByBillType(int type)
        {
            return dal.GetLKBillTemplateByBillType(type);
        }
    }
}

